package com.ken.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ken.constant.MessageConstant;
import com.ken.constant.StatusConstant;
import com.ken.context.BaseContext;
import com.ken.dto.SetmealDTO;
import com.ken.entity.Setmeal;
import com.ken.entity.SetmealDish;
import com.ken.exception.DeletionNotAllowedException;
import com.ken.mapper.DishMapper;
import com.ken.mapper.MealDishMapper;
import com.ken.mapper.SetmealMapper;
import com.ken.service.MealDishService;
import com.ken.service.MealService;
import com.ken.vo.DishItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class MealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements MealService {
    @Autowired
    private MealDishService mealDishService;


    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private MealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public void updateSetMealAndMealDish(SetmealDTO setmealDTO) {
        Long currentId = BaseContext.getCurrentId();
        Setmeal setmeal = BeanUtil.copyProperties(setmealDTO, Setmeal.class);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(currentId);
        save(setmeal);
        Long id = setmealDTO.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        mealDishService.remove(
                new LambdaQueryWrapper<SetmealDish>()
                        .eq(SetmealDish::getSetmealId,id));
        if(setmealDishes!=null&& !setmealDishes.isEmpty()){
            List<SetmealDish> setmealDishes1=new ArrayList<>();
            for (SetmealDish setmealDish:setmealDishes){
                SetmealDish setmealDish1=SetmealDish.builder()
                        .setmealId(setmealDish.getSetmealId())
                        .id(setmealDish.getId())
                        .dishId(setmealDish.getDishId())
                        .name(setmealDish.getName())
                        .price(setmealDish.getPrice())
                        .copies(setmealDish.getCopies())
                        .build();
                setmealDishes1.add(setmealDish1);
            }
            mealDishService.saveBatch(setmealDishes1);
        }
    }

    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        ids.forEach(id-> {
            Setmeal setmeal=getById(id);
            if (StatusConstant.ENABLE==setmeal.getStatus()){
                throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        ids.forEach(setmealId->{
            removeById(setmealId);
            mealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,setmealId));
        });
    }


    @Transactional
    @Override
    public void insetMealAndDish(SetmealDTO setMealDTO) {
        //获取当前用户
        Long currentId = BaseContext.getCurrentId();
        //复制Bean
        Setmeal setmeal = BeanUtil.copyProperties(setMealDTO, Setmeal.class);
        //基本信息
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(currentId);
        setmeal.setUpdateUser(currentId);
        setmeal.setStatus(StatusConstant.ENABLE);
        save(setmeal);
        //写入setmeal_dish
        List<SetmealDish> setMealDishes = setMealDTO.getSetmealDishes();
        if (setMealDishes!=null && !setMealDishes.isEmpty()) {
            List<SetmealDish> list = new ArrayList<>();
            for (SetmealDish mealDishes : setMealDishes){
                SetmealDish mealDish =SetmealDish.builder()
                        .setmealId(mealDishes.getSetmealId())
                        .dishId(mealDishes.getId())
                        .dishId(mealDishes.getDishId())
                        .name(mealDishes.getName())
                        .price(mealDishes.getPrice())
                        .copies(mealDishes.getCopies())
                        .build();
                list.add(mealDish);
            }
            mealDishService.saveBatch(list);
        }
    }
}
